package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: TransactionController
 * @Package: com.huiminpay.transaction.controller
 * @Description:
 * @Author 谢同学
 * @Create 2023/11/27 12:58
 * @Version 1.0
 */
@Log4j2
@RestController
public class TransactionController {

    //必要参数

    //应用id
    String APP_ID = "9021000132634068";
    //应用私钥
    String APP_PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVcO4TU0IN9hP7KLVzbWn4wGDqIdWCahD1PNw3HY4E3PE2VNKLnqeTPPTtcUXyOFxtUkVEhtCE5LALff0Wfeuf9hz9iJ6+R1IBG7nqsI3k2olT10itzFo48pIETLU1hXNFHvBi5k40kciXJF0LvGQNmTEqFuFBLgZPDVyUGU9wWU/Xpdy2oVemNXNEnlfixXrjm1FF8ti4EfW2A6bP6QeoIGK2K2a7BDWIDGPmVRxMnJ0XWoph+maFKRwa+VctDscmB0GbJD/AGWPwd7VUOQ+KiGFKlO729RxJzLsFuN3nLZgxNdIeNdhVcibpsju8eYezl6QurP0PMxQvPW6QOjEpAgMBAAECggEAMJselGKMJYZYUWSYQAbiZOoNm5mNnHFrOpik97hBLQu/2PAV2xurXZSCBYjGO1ZhmZ0csoE26UKIj/RWkS47qTefjkcVesYvwfSGr/Lk8/6wk/h9xymkXslm/McGWXYiW+UIQuKtpSOVnrNRIDQSRuoAt7PhaBXUL3olFnZ1BBFFwzVeHvnuph7u4hy2IXIx/2hu9qk3a07wlyo8b7WJeZDz5RpAgb2j9JJBG6Xjxgm5PJH+/p40mkyycE/05kLmNd4H2GH5XhLymuzMJST7ETM2byY5Ek0FfT2qPXe38UTogwV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Nu7dnxhlGuvJiMqt+oj7FMtdumbvfRMd1ry2oM0DA9EcO1c+it5b8LZhnLY2D7jwU+4hR598b5ETOVT5Prdwbt0z/p1hyyxGgkTMjMTnI3KHN76a1w611H7NW2SzB9iwyS7RXipvsWL6ct1plPvlvjJsuZJ7cFxVh3sCEdGYKPvlX5xyV6ikA8rLNEmt1HGP8D/56bK/S1a6LalkC+a6JUwJbVUkqH+PMoet8LfoXgs0fJOxG5AhwS0MaS6oC9YC1qL1s/gkosofzRwIDAQAB";
    //编码
    String CHARSET = "utf-8";
    //网关地址 正式"https://openapi.alipay.com/gateway.do"
    String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";//正式"https://openapi.alipay.com/gateway.do"

    @GetMapping("alipayTest")
    public String alipayTest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        //获得初始化的AlipayClient
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET,ALIPAY_PUBLIC_KEY,"RSA2");
        //构建请求
        AlipayTradeWapPayRequest payRequest = new AlipayTradeWapPayRequest();

        payRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101123\"," +
                " \"total_amount\":\"20000\"," +
                " \"subject\":\"给张宇点20个女仆\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数

        //调用SDK生成表单
        String form;
        try {
            form= alipayClient.pageExecute(payRequest).getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }

        httpServletResponse.setContentType("text/html;charset=" + CHARSET);
        httpServletResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();


        return form;
    }
}
